﻿using System;
using System.Collections.Generic;
using System.Text;
using NLBS.Entity;
using NLBS.Data.Business;
using NLBS.Framework;
using NLBS.Data;

namespace NLBS.Views
{
    public interface IContentView
    {
        string q { get; }
        int cat { get; set; }
        int user { get; set; }
        string date { get; set; }
        string selected { get; set; }
        int startIndex { get; set; }
        int pageSize { get; set; }
        string pageUrl { get; set; }
        string tag { get; }
        IPagedCollection<Article> artList { get; set; }
    }
   public class ContentPresenter:Presenter<IContentView>
    {
       public override void OnViewLoaded()
       {
           if(!string.IsNullOrEmpty(View.q))
           {
               View.pageUrl = "javascript:NLBSPage.ArticleCall('{0}','q','" + View.q + "')";
               View.artList = DbProvider.Instance().GetPagedArticleBySearch(this.View.startIndex, this.View.pageSize, View.q);
               return;
           }

           if (!string.IsNullOrEmpty(View.tag))
           {
               View.pageUrl = "javascript:NLBSPage.ArticleCall('{0}','tag','" + View.tag + "')";
               View.artList = DbProvider.Instance().GetArticleByTag(this.View.startIndex, this.View.pageSize, View.tag);
               return;
           }

           if (View.cat > 0)
           {
               View.pageUrl = "javascript:NLBSPage.ArticleCall('{0}','cat'," + View.cat + ")";
               View.artList = ArticleData.GetPageArticleByCatID(this.View.startIndex, this.View.pageSize, View.cat);
           } 
           else if (View.user > 0)
           {
               View.pageUrl = "javascript:NLBSPage.ArticleCall('{0}','user'," + View.user + ")";
               View.artList = ArticleData.GetPageArticleByUserID(this.View.startIndex, this.View.pageSize, View.user);
           } 
           else if (!string.IsNullOrEmpty(View.selected))
           {
               View.pageUrl = "javascript:NLBSPage.ArticleCall('{0}','selected',true)";
               View.artList = ArticleData.GetPageArticleBySelected(this.View.startIndex, this.View.pageSize);
           }
           else if (!String.IsNullOrEmpty(View.date))
           {
               View.pageUrl = "javascript:NLBSPage.ArticleCall('{0}','date','" + View.date + "')";
               string[] dateStr = Convert.ToString(View.date).Split('-');

               if (dateStr.Length == 1)
               {
                   View.artList = ArticleData.GetArticleByYear(this.View.startIndex, this.View.pageSize, Convert.ToInt32(dateStr[0]), CacheDuration.Long);
               }
               else if (dateStr.Length == 2)
               {
                   View.artList = ArticleData.GetArticleByMonth(this.View.startIndex, this.View.pageSize, Convert.ToInt32(dateStr[0]), Convert.ToInt32(dateStr[1]), CacheDuration.Long);
               }
               else if (dateStr.Length == 3)
               {
                   View.artList = ArticleData.GetArticleByDay(this.View.startIndex, this.View.pageSize, Convert.ToDateTime(View.date), CacheDuration.Long);
               }
           }
           else
           {
               View.pageUrl = "javascript:NLBSPage.ArticleCall('{0}')";
               View.artList = ArticleData.GetPageArticle(this.View.startIndex, this.View.pageSize, CacheDuration.Long);
           }
           base.OnViewLoaded();
       }

    }
}
